﻿using Newtonsoft.Json;
using SqlSugar;
using Sunny.UI;
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using WCS.Entity;
using WCS.Utils;

namespace WCS.WebRequest
{
    /// <summary>
    /// 
    /// </summary>
    public class HttpRequest
    {
        private HttpRequest() { }

        private static readonly HttpRequest _httpRequest = new HttpRequest();

        public static HttpRequest httpRequest
        {
            get
            {
                return _httpRequest;
            }
        }
        static SqlSugarDapper sqlSugarDapper = SqlSugarDapper.GetDbContext();

        string prefix = DateTime.Now.ToString().Substring(0, 8).Replace("/", "");

        /// <summary>
        /// WCS===Wms接口日志删除时间  删除多少天以前的数据
        /// </summary>
        public int DeleteTime = Convert.ToInt32(ConfigurationManager.AppSettings["DeleteTime"]);

        /// <summary>
        /// 调用接口POST请求
        /// </summary>
        /// <param name="httpContent"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public Result WebPostRequest(string httpContent, string url, string Authorization = null)
        {
            try
            {
                //创建请求对象
                using (HttpClient httpClient = new HttpClient())
                {
                    //设置服务器响应时间
                    httpClient.Timeout = TimeSpan.FromSeconds(10);
                    //if (Authorization != null)
                    {
                        httpClient.DefaultRequestHeaders.Add("token", "admin123");
                        httpClient.DefaultRequestHeaders.Add("name", "lx-script");
                    }

                    //设置传入的参数，请求格式
                    var str = new StringContent(httpContent, Encoding.UTF8, "application/json");

                    //发送请求,填入请求地址并接受相应数据
                    string response = httpClient.PostAsync(url, str).Result.Content.ReadAsStringAsync().Result;

                    var result = JsonConvert.DeserializeObject<Result>(response);
                    return result;
                }
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");

                return null;
            }
        }

        public Results WebPostRequestMES(string httpContent, string url)
        {
            try
            {
                //创建请求对象
                using (HttpClient httpClient = new HttpClient())
                {
                    //设置服务器响应时间
                    httpClient.Timeout = TimeSpan.FromSeconds(10);

                    //设置传入的参数，请求格式
                    var str = new StringContent(httpContent, Encoding.UTF8, "application/json");

                    //发送请求,填入请求地址并接受相应数据
                    string response = httpClient.PostAsync(url, str).Result.Content.ReadAsStringAsync().Result;

                    var result = JsonConvert.DeserializeObject<Results>(response);
                    return result;
                }
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");

                return null;
            }
        }

        public Resultss WebPostRequestMESS(string httpContent, string url)
        {
            try
            {
                //创建请求对象
                using (HttpClient httpClient = new HttpClient())
                {
                    //设置服务器响应时间
                    httpClient.Timeout = TimeSpan.FromSeconds(10);

                    //设置传入的参数，请求格式
                    var str = new StringContent(httpContent, Encoding.UTF8, "application/json");

                    //发送请求,填入请求地址并接受相应数据
                    string response = httpClient.PostAsync(url, str).Result.Content.ReadAsStringAsync().Result;

                    var result = JsonConvert.DeserializeObject<Resultss>(response);
                    return result;
                }
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");

                return null;
            }
        }

        public Resultmes WebGetRequestMES(string httpContent, string url)
        {
            try
            {
                //创建请求对象
                using (HttpClient httpClient = new HttpClient())
                {
                    //设置服务器响应时间
                    httpClient.Timeout = TimeSpan.FromSeconds(10);

                    //发送请求,填入请求地址并接受相应数据
                    string response = httpClient.GetAsync(url + httpContent).Result.Content.ReadAsStringAsync().Result;

                    return JsonConvert.DeserializeObject<Resultmes>(response);
                }
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");
                return null;
            }
        }

        /// <summary>
        /// 调用接口POST请求
        /// </summary>
        /// <param name="httpContent"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetToken()
        {
            try
            {
                var obj = new
                {
                    username = ConfigurationManager.ConnectionStrings["userName"].ConnectionString,
                    password = ConfigurationManager.ConnectionStrings["passWord"].ConnectionString,
                    captcha = ConfigurationManager.ConnectionStrings["captcha"].ConnectionString
                };

                WebPostRequest(JsonConvert.SerializeObject(obj), "");
                return "";
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");

                return null;
            }
        }

        /// <summary>
        /// 删除指定时间之前的日志
        /// </summary>
        public void DeleteApiLog()
        {
            sqlSugarDapper.DB.Deleteable<ApiLogs>().Where(w => w.CreateTime < DateTime.Now.AddDays(DeleteTime)).ExecuteCommand();
        }


        /// <summary>
        /// 生成子任务号
        /// </summary>
        /// <returns></returns>
        private static string GenerateRandomCode()
        {

            var result = new StringBuilder();
            for (var i = 0; i < 10; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }

        /// <summary>
        /// 调用接口GET请求
        /// </summary>
        /// <param name="httpContent"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public Result WebGetRequest(string httpContent, string url)
        {
            try
            {
                //创建请求对象
                using (HttpClient httpClient = new HttpClient())
                {
                    //设置服务器响应时间
                    httpClient.Timeout = TimeSpan.FromSeconds(10);

                    httpClient.DefaultRequestHeaders.Add("token", "admin123");
                    httpClient.DefaultRequestHeaders.Add("name", "lx-script");

                    //发送请求,填入请求地址并接受相应数据
                    string response = httpClient.GetAsync(url+ httpContent).Result.Content.ReadAsStringAsync().Result;

                    //DeleteApiLog();

                    return JsonConvert.DeserializeObject<Result>(response);
                }
            }
            catch (Exception ex)
            {
                //ShowLog.showLog.Error($@"异常信息【{ex.Message}】【{ex.StackTrace}】");
                return null;
            }
        }
    }

    /// <summary>
    /// 返回结果实体
    /// </summary>
    public class Result
    {
        /// <summary>
        /// 返回代码
        /// </summary>
        public string errCode { get; set; }

        /// <summary>
        /// 返回信息  
        /// </summary>
        public string errMsg { get; set; }

        /// <summary>
        /// 返回代码
        /// </summary>
        public bool state { get; set; }

        /// <summary>
        /// 返回信息 
        /// </summary>
        public string data { get; set; }
    }

    public class Results
    { 
        public int ret { get; set; }
        public int msg { get; set; }
        public string data { get; set; }

    }
    public class Resultss
    {
        public int ret { get; set; }
        public int msg { get; set; }
        public datas data { get; set; }

    }

    public class datas
    {
        public int code { get; set; }
        public string displayMsg { get; set; }
    }

    public class Resultmes
    {
        public int ret { get; set; }
        public int msg { get; set; }

        public data data { get; set; }
    }

    public class data
    {
        /// <summary>
        /// 库存状态
        /// </summary>
        public string trayResult { get; set; }

        public string trayResultDesc { get; set; }

    }
    /// <summary>
    /// 返回结果实体
    /// </summary>
    public class RequestTask
    {
        /// <summary>
        /// 请求条码
        /// </summary>
        public string trayCode { get; set; }

        /// <summary>
        /// 当前位置
        /// </summary>
        public string currentLocation { get; set; }
    }
}
